<?php 
class Msms extends CI_Model{
	function __construct(){
		parent::__construct();
	}

    /**
     * 获取用户消息
     * @param $uid
     * @param int $pageSize
     * @return string
     */
    function getUserSms($uid,$type,$pageSize=15){

        $where = ' `username`='.$uid;
        if($type == 1){
            $where .= ' and `is_read`=1';
        }elseif($type == 0){
            $where .= ' and `is_read`=0';
        }

        $query = '';
        $order = ' id desc ';
        $sqlCount = $this->db->query("select `id` from `tbsms` where $where");
        $count = $sqlCount->num_rows();

        $sqlCount2 = $this->db->query("select `id` from `tbsms` where `is_read`=0 and `username`= $uid");
        $count2 = $sqlCount2->num_rows();

        if($count > 0){
            $sql = "select * from `tbsms` where $where order by $order limit ";

            $query = $this->db->pager($count,$pageSize,$sql);
        }
        $query['count'] = isset($count2) ? $count2 :0;
        $query['data'] = isset($query['data']) ? $query['data'] :0;
        return $query;
    }

	
	function getSmsById($id){		
		$sql = "select *
				from tbsms
				where id = ?";
		$query = $this->db->query($sql,array("$id"));
		$arr = array();
		foreach ($query->result() as $row){
			$arr['id'] = $id;
			$arr['title'] = $row->title;
			$arr["content"] = $row->content;
			$arr["remark"] = $row->remark;
			$arr["username"] = $row->username;
			$arr["groupidstr"] = $row->groupidstr;
			$arr["createtime"] = $row->createtime;			
		}
		return $arr;
	}

    function setSmsRead($id)
    {
        $data = array(
            'is_read' => 1,
        );

        $this->db->where('id', $id);
        $this->db->update('tbsms', $data);

    }

}
?>